<?php
namespace Com\Pay;
class NsXunda{

    private $config = array();


    public function __construct($send_data){
        $this->config = $send_data;
    }

    /*生成支付连接*/
    public function getUrl($order_info){
        $price = $order_info['paymoney'];
        $notifyUrl = 'http://' . $_SERVER['HTTP_HOST'] . '/Api/NotifyCallback/NsXundaNotify?mch_id=' . $this->config['mch_id'];
        $order_sn = $order_info['ordernumber'];
        $post_data = array(
            'userid' =>   $this->config["mch_id"],
            'nonce_str' => substr(md5(rand(10000 , 999999)) , 10),
            'body' => 'ChangbaoPay',
            'amount'    =>  $price,
            'route' => 0,
            'notifyUrl'    => $notifyUrl,
            'callback_url' => $order_info['return_url'],
            'pay_number' => $order_sn,
            'orderCode' => $order_info['orderCode']
        );

        $ContentSign = $this->setParam($post_data,$this->config['mch_key'],false); // 计算出Sign
        $post_data['sign'] = $ContentSign; // Sign签名
        $tar_url = "http://extman.kefupay.cn/pay/pay_mobile.action";
        $res = json_decode($this->https_request($tar_url,$post_data) , true);
        if($res['payUrl'] != "000000"){
            return $res['payUrl'];
        }
        return '';
    }


    private function setParam($param,$key,$IS_Notify){
        if(!$IS_Notify) {
            $md5s = array(
                'amount' => $param['amount'],
                'body' => $param['body'],
                'nonce_str' => $param['nonce_str'],
                'notifyUrl' => $param['notifyUrl'],
                'route' => $param['route'],
                'callback_url' => $param['callback_url'],
                'userid' => $param['userid'],
            );
            ksort($md5s);
        }else{
            $md5s = array(
                'amount' => $param['amount'],
                'orderId' => $param['orderId'],
                'pay_number' => $param['pay_number'],
            );
        }
        $str = "";
        foreach ($md5s as $k => $v){
            $str.= $k . '=' . $v . '&';
        }
        $str1 = rtrim($str , '&');
        $sign = md5($str1 . '&key=' . $key);
        return $sign;
    }

    //回调方法，把返回数据处理成统一规定返回
    public function response(){
        $data = $_REQUEST;
        $MySign = $this->setParam($data,$this->config['mch_key'],true);
        if($data['sign'] == $MySign){
            if($data['trade_state'] == 'SUCCESS'){
                return true;
            }else{
                // 失败
                return false;
            }
        }else{
            // 失败
            return false;
        }
    }


    private function https_request($url, $data = null){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

        //参数不为空则为post请求

        if (!empty($data)){

            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

}
